/*!
 * \file frmem.h
 *
 * \brief 内存管理
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * 相关接口，实现内存管理
 *
 * \version 2.0
 * \author yaohl
 * \date 2014/09/29
 *
 */

#ifndef __FRMEM_H_
#define __FRMEM_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "frmem_cfg.h"

/* 内存大小  52428800 (50*1024*1024) */
#define FR_MEM_BUFF_SIZE    52428800

/* 内存检查 */
#if FR_MEM_LEAK_CHECK
    #include "frmem_chk.h"

/* 标准内存分配函数 */
#else
    int  __fr_mem_init(size_t s_size, const char *p_slab_name);
    void __fr_mem_stat(char **pp_outbuff);
    void __fr_mem_destroy(void);

    #define __frcomm_slab_init(x, y)    __fr_mem_init(x, y)
    #define __frcomm_slab_stat(x)       __fr_mem_stat(x)
    #define __frcomm_slab_destroy()     __fr_mem_destroy()

    void *__fr_memdup(const char *p_src, size_t s_size);

    #define __frcomm_malloc(x)       malloc(x)
    #define __frcomm_memdup(x,y)     __fr_memdup(x, y)
    #define __frcomm_strdup(x)       strdup(x)
    #define __frcomm_free(x)         free(x)
#endif


#if FR_MEM_STAT
    #include "frmem_stat.h"

    //#define fr_slab_init(x, y)  __fr_memstat_init(x, y)
    //#define fr_slab_init2(x, y)  __fr_memstat_init2(x, y)
    //#define fr_slab_init3(x, y)  __fr_memstat_init3(x, y)
    #define fr_slab_init(x, y)  __fr_memstat_init3(x, y)
    #define fr_slab_set(x)      __fr_memstat_set(x)

    #define fr_slab_stat(x)     __fr_memstat_info_all(x)
    #define fr_slab_all_used()  __fr_memstat_all_used()
    #define fr_slab_destroy()   __fr_memstat_destroy()

    #define frmalloc(x)       __fr_memstat_malloc(x)
    #define frstrdup(x)       __fr_memstat_strdup(x)
    #define frmemdup(x,y)     __fr_memstat_memdup(x, y)
    #define frfree(x)         __fr_memstat_free(x)
#else
    /* 不需要统计信息 */
    #define fr_slab_init(x, y)  __frcomm_slab_init(x, y)
    #define fr_slab_init2(x, y)  (void *)1;__frcomm_slab_init(x, y)
    #define fr_slab_set(x)

    #define fr_slab_stat(x)     __frcomm_slab_stat(x)
    #define fr_slab_all_used()  0
    #define fr_slab_destroy()   __frcomm_slab_destroy()

    #define frmalloc(x)       __frcomm_malloc(x)
    #define frstrdup(x)       __frcomm_strdup(x)
    #define frmemdup(x,y)     __frcomm_memdup(x, y)
    #define frfree(x)         __frcomm_free(x)
#endif

#endif

